<?php

namespace maotool\core;

use maotool\core\annotation\old\RequestMapping;
use maotool\core\annotation\old\RestController;
use maotool\match\AntPathMatcher;
use maotool\object\ReflectionUtil;
use maotool\util\AntPathMatcherUtil;

class MaoToolApplication
{

    /**
     * @param $concrete
     * @param $args
     * @return MaoToolApplication
     */
    public static function run($concrete, $args)
    {
        \maotool\log\Console::debug("程序启动中");
        \maotool\log\Console::debug("程序入参", $args);
        $instance = Container::set(["concrete" => $concrete, "args" => $args]);
        $InstanceControllers = Container::getInstanceByAnnotations(RestController::class);
        foreach ($InstanceControllers as $instanceController) {
            foreach ($instanceController->annotations as $annotation) {
                if (ReflectionUtil::getClass($annotation) == RestController::class) {
                    $methods = ReflectionUtil::getMethods($instanceController->concrete, true, "object");
                    foreach ($methods as $method) {
                        $requestMapping = ReflectionUtil::getAnnotationByClass($method, RequestMapping::class);
                        if ($requestMapping) {
                            /** @var RestController $annotation */
                            if (AntPathMatcherUtil::match($annotation->value[0] . $requestMapping->value[0], "/member/index")) {
                                $methodName = $method->name;
                                $result = $instanceController->proxy->$methodName();
                                \maotool\log\Console::debug($result);
                            }
                        }
                    }
                }
            }
        }
        return $instance->proxy;
    }

    public function exit()
    {
        \maotool\log\Console::debug("程序已退出");
    }

}